package com.ruicar.afs.cloud.common.voucher.handle;

import cn.hutool.crypto.SecureUtil;
import com.ruicar.afs.cloud.common.voucher.Voucher;
import com.ruicar.afs.cloud.common.voucher.VoucherBaseInfo;
import com.ruicar.afs.cloud.common.voucher.VoucherGroup;
import lombok.experimental.UtilityClass;
import org.springframework.util.Assert;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@UtilityClass
public  class VoucherCaches {
    private static final String FORMAT_STR = "编号为 [{0}] 的 [{1}] [{2}]";
    private static final Map<String, Voucher> VOUCHER_CACHE_MAP = new HashMap(64);
    private static final Map<String, VoucherGroup> VOUCHER_GROUP_MAP = new HashMap(32);
    private static final Map<String, String> VOUCHER_NO_CHECK = new HashMap(32);
    private static final Map<String,List<Long>>  VOUCHER_BURIEDPOINT_NO= new HashMap<>(32);
    private static final Map<Long, VoucherBaseInfo>  VOUCHER_INFO= new HashMap<>(32);

     static void addVoucher(String ruleNo, Voucher voucher) {
        Assert.isTrue(!VOUCHER_CACHE_MAP.containsKey(ruleNo), MessageFormat.format(FORMAT_STR, ruleNo, "凭证", "已存在,不能重复添加"));
         VOUCHER_NO_CHECK.put(ruleNo, SecureUtil.md5(voucher.getRuleExpress()));
         VOUCHER_CACHE_MAP.put(ruleNo, voucher);
    }
    static void addBuriedPointNo(Map<String,List<Long>>  map) {
        VOUCHER_BURIEDPOINT_NO.putAll(map);
    }

    static void addVoucherBaseInfo(Long voucherNO,VoucherBaseInfo info) {
        VOUCHER_INFO.put(voucherNO, info);
    }

    static void removeRule(String ruleNo) {
        VOUCHER_CACHE_MAP.remove(ruleNo);
    }

    static void addGroup(String groupNo, VoucherGroup group) {
        Assert.isTrue(!VOUCHER_GROUP_MAP.containsKey(groupNo), MessageFormat.format(FORMAT_STR, groupNo, "规则组", "已存在,不能重复添加"));
        VOUCHER_GROUP_MAP.put(groupNo, group);
    }

    static void removeGroup(String groupNo) {
        VOUCHER_GROUP_MAP.remove(groupNo);
    }

    public static Voucher getVoucher(String ruleNo) {
        Assert.isTrue(VOUCHER_CACHE_MAP.containsKey(ruleNo), MessageFormat.format(FORMAT_STR, ruleNo, "凭证不存在", "不存在"));
        return (Voucher) VOUCHER_CACHE_MAP.get(ruleNo);
    }

    public static VoucherGroup getRuleGroup(String ruleGroupNo) {
        Assert.isTrue(VOUCHER_GROUP_MAP.containsKey(ruleGroupNo), MessageFormat.format(FORMAT_STR, ruleGroupNo, "规则组", "不存在"));
        return (VoucherGroup) VOUCHER_GROUP_MAP.get(ruleGroupNo);
    }

    public static List<Voucher> getVouchers(List<String> ruleNos) {
        return (List) VOUCHER_CACHE_MAP.entrySet().stream().filter((entry) -> {
            return ruleNos.contains(entry.getKey());
        }).map((entry) -> {
            return (Voucher) entry.getValue();
        }).collect(Collectors.toList());
    }

    public static List<Long> getEvents(String buriedpointNo) {
        Assert.isTrue(VOUCHER_BURIEDPOINT_NO.containsKey(buriedpointNo), MessageFormat.format(FORMAT_STR, buriedpointNo, "埋点场景", "不存在"));
        return (List<Long>) VOUCHER_BURIEDPOINT_NO.get(buriedpointNo);
    }
    public static VoucherBaseInfo getVoucherInfo(Long id) {
        Assert.isTrue(VOUCHER_INFO.containsKey(id), MessageFormat.format(FORMAT_STR, id, "凭证模板不存在", "不存在"));
        return (VoucherBaseInfo) VOUCHER_INFO.get(id);
    }

    public static Map<String, String> getChecks() {
        return VOUCHER_NO_CHECK;
    }

    public static void resetCache() {
        VOUCHER_BURIEDPOINT_NO.clear();
        VOUCHER_INFO.clear();
        VOUCHER_GROUP_MAP.clear();
        VOUCHER_CACHE_MAP.clear();
        VOUCHER_NO_CHECK.clear();
    }



}
